<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/model/Model_Mysql.php');

class Model_User_Notifications extends Model_Mysql {
	const FEED = 1;
	const COMMENT = 2;
	const FRIEND_REQ = 3;
	const FEIEND_ACCEPTED = 4;
	
	const STATUS_NEW = 1;
	const STATUS_SEEN = 2;
	const STATUS_RESPONDED = 3;
	const STATUS_DELETED = 4;
	
	public function addNotification($uid, $type, $data) {
		$sql = "INSERT INTO user_notifications(uid,type,data,time,status)
			VALUES($uid, $type, $data, ".time().", ".self::STATUS_NEW.")";
		$query = DB::query(Database::INSERT, $sql);
		return $this->execute($query);
	}
	public function addByFeed($feedid, $uids) {
		$now = time();
		$uids_str = implode(",", $uids);
		$sql = "INSERT INTO user_notifications(uid,type,data,time,status) SELECT
				postedby,".self::COMMENT.",feedid,$now,".self::STATUS_NEW." FROM
				users_feed WHERE feedid=$feedid AND postedby NOT IN ($uids_str)";
		$query = DB::query(Database::INSERT, $sql);
		return $this->execute($query);
	}
	public function getNotifications($uid) {
		$sql = "SELECT * FROM user_notifications WHERE uid = $uid";
		$query = DB::query(Database::SELECT, $sql);
		return $this->execute($query);
	}
}